package org.colcon.web.client.ui;

import org.colcon.web.client.Collective_Congress;
import org.colcon.web.model.framework.IItem;
import org.colcon.web.model.framework.ILink;
import org.colcon.web.model.framework.ILinkContainer;
import org.colcon.web.model.parsers.CLIParser;

import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Label;

public class LinkContainerPanel extends ItemPanel 
{
	public LinkContainerPanel(Collective_Congress client, ILinkContainer container) 
	{
		super(client, container);
		
		final String path = container.getPath();
		
		getService().open(path, new AsyncCallback<String[]>()
		{
			public void onFailure(Throwable caught)
			{
				add(new Label("failed to open container '" + path + "'"));
			}
			
			public void onSuccess(String[] array)
			{
				for(String str : array)
				{
					IItem item = CLIParser.parseItem(str);
						
					if(item == null || !(item instanceof ILink))
						continue;
					
					ILink link = (ILink) item;
					
					add(new LinkPanel(getClient(), "link-path", link.getLink(), link.getLink()));
				}
			}
		});
	}
	
	public ILinkContainer getContainer()
	{
		return (ILinkContainer) getItem();
	}
}
